package com.wondertek.mist.plugins;


import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;

import com.wondertek.mist.gallery.ImageGallery;

public class GalleryPlugin extends CordovaPlugin {

	public static final String MIST_PHOTO_EXTRA = "mist.photo.extra";
	
	@Override
	public boolean execute(String action, JSONArray args,
			CallbackContext callbackContext) throws JSONException {
		String message = args.getString(0);                 // 这里接收页面参数，按传参的顺序
		
		initPhotoView(message);
		return true;
	}

	public void initPhotoView(final String images) {
		final Activity cvdActivity = this.cordova.getActivity();
		
		Runnable runnable = new Runnable() {
            public void run() {
            	Intent intent = new Intent(cvdActivity,ImageGallery.class);
            	intent.putExtra(MIST_PHOTO_EXTRA, images);
            	cvdActivity.startActivity(intent);
            };
        };
        
        // Activity类的runOnUiThread (Runnable action)函数，
        //这个函数的主要功能：在UI线程中运行指定的操作，如果当前线程是UI线程，然后采取行动立即执行；
        //如果当前线程不是UI线程，发送 消息到UI线程的事件队列。
        cvdActivity.runOnUiThread(runnable);

	}
}
